/**
 * Copyright (c) 2010 XXXXX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.edu.itba.ia2010.geneticos.motor.api.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.Validate;

import ar.edu.itba.ia2010.geneticos.motor.api.Cromosoma;
import ar.edu.itba.ia2010.geneticos.motor.api.Gen;
import ar.edu.itba.ia2010.geneticos.motor.api.impl.BooleanGen;
import ar.edu.itba.ia2010.geneticos.motor.api.impl.ListCromosoma;

/**
 * Clases de utilidad 
 * 
 * 
 * @author Juan F. Codagnone
 * @since Mar 21, 2010
 */
public final class GeneUtils {

    /** utility class */
    private GeneUtils() {
        // void
    }
    
    /** valida si probability est*/
    public static void validateProbabilidad(final double probability) 
       throws IllegalArgumentException {
        Validate.isTrue(probability >= 0 && probability < 1, 
            "probabilidad debe pertenecer a [0, 1)");
    }
    
    
    /** 
     * Crea un gen a partir de una cadena de caracteres.  En este caso crea
     * un cromosoma boolean a partir de '0' y '1's.
     * Ej:
     * "01101"
     */
    public static Cromosoma<Boolean> valueOf(final String bits) {
        Validate.notNull(bits);
        final List<Gen<Boolean>> genes = new ArrayList<Gen<Boolean>>(bits.length());
        
        for(int i = 0; i < bits.length(); i++) {
            genes.add(BooleanGen.valueOf(bits.charAt(i)));
        }
        return new ListCromosoma<Boolean>(genes);
    }
}
